package com.lanwf.admin.sys.ue;

import com.lanwf.admin.BaseController;
import com.jfinal.aop.Clear;
import com.jfinal.core.Path;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import com.lanwf.common.upload.ImageKit;
import com.lanwf.admin.utils.Cons;

/**
 * UploadController 上传控制器，接管 ueditor 上传功能
 */
@Path(value = "/common/upload", viewPath = "")
public class UploadController extends BaseController {
	
	/**
	 * 接管 ueditor 上传图片服务端
	 *
	 * 1：该 action 与 ueditor.config.js 中的 serverUrl: "/upload/ueditor" 对应
	 *
	 * 2：ueditor 页面加载时会向后端发送 "/xxx?action=config 请求用来获取服务端
	 *    /ueditor-home/jsp/config.json 中的配置，后续的上传将受该配置的影响
	 *
	 * 3：ueditor1_4_3_2-utf8-jsp 版本测试上传图片成功所返回的 json 数据格式如下：
	 *  {
	 *     "state": "SUCCESS",
	 *     "title": "1461249851191086496.png",
	 *     "original": "qr.png",
	 *     "type": ".png",
	 *     "url": "/ueditor/jsp/upload/image/20160421/1461249851191086496.png",
	 *     "size": "58640"
	 *  }
	 *
	 * 4：如果上传出现错误，直接响应如下的 json 即可：
	 *    {"state": "错误信息"}
	 *
	 */
	/*public void ueditor() {
		System.err.println("*** >>> ue" );
		*//**
		 * ueditor 在页面加载时会向后端请求获取 config.json 内容
		 *//*
		if ("config".equals(getPara("action"))) {
			render("/assets/ueditor/jsp/config.json");
			return;
		}
		*//**
		 * 对应 config.json 配置的 imageActionName: "uploadimage"
		 *//*
		if ( ! "uploadimage".equals(getPara("action"))) {
			renderJson("state", "UploadController 只支持图片类型的文件上传");
			return ;
		}
		*//**
		 * uploadType 是通过如代码令 ueditor 在上传图片时通过问号挂参的方式传递过来的自定义参数
		 * ue.ready(function() {
		 *      ue.execCommand('serverparam', {
		 *          'uploadType': 'project'
		 *      });
		 * });
		 *//*
		String uploadType = getPara("uploadType");
		if (StrKit.isBlank(uploadType)) {
			renderJson("state", "上传类型参数缺失");
			return ;
		}

*//*		if (notLogin()) {
			renderJson("state", "只有登录用户才可以上传文件");
			return ;
		}*//*

		UploadFile uploadFile = null;
		try {
			// "upfile" 来自 config.json 中的 imageFieldName 配置项
			uploadFile = getFile("upfile", UploadService.uploadTempPath, UploadService.imageMaxSize);
			Ret ret = srv.ueditorUpload("", uploadType, uploadFile);
			// renderJson(ret);
			render(new JsonRender(ret).forIE());	// 防止 IE 下出现文件下载现象
		}
		catch(com.jfinal.upload.ExceededSizeException ex) {
			renderJson("state", "上传图片只允许 200K 大小");
		}
		catch(Exception e) {
			if (uploadFile != null) {
				uploadFile.getFile().delete();
			}
			
			renderJson("state", "上传图片出现未知异常，请告知管理员：" + e.getMessage());
			LogKit.error(e.getMessage(), e);
		}
	}
*/
	@Clear
	public void ueditor( ) {

		if ("config".equals(getPara("action"))) {
			render("/assets/ueditor/jsp/config.json");
			return;
		}
/*		String uploadType = getPara("uploadType");
		if (StrKit.isBlank(uploadType)) {
			renderJson("state", "上传类型参数缺失");
			return;
		}*/

		UploadFile uploadFile = null;
		try {
			// "upfile" 来自 config.json 中的 imageFieldName 配置项

			String uploadPath = "";
			Integer maxPostSize = 51200000;
			if ("uploadimage".equals(getPara("action"))) {
				uploadPath = "/ue/img/";
				maxPostSize = 1024 * 1024 * 10;
			} else if ("uploadvideo".equals(getPara("action"))) {
				uploadPath = "/ue/video/";
				maxPostSize = 1024 * 1024 * 50;
			} else if ("uploadfile".equals(getPara("action"))) {
				uploadPath = "/ue/file/";
				maxPostSize = 1024 * 1024 * 20;
			}

			uploadFile = getFile("upfile", uploadPath, maxPostSize);
            long size = uploadFile.getFile( ).length( );
            if(size > maxPostSize && uploadPath.equals("/ue/img/")){
                renderJson("state", "图片太大请控制在10MB以内");
                uploadFile.getFile().delete();
                return;
            }
            if(size > maxPostSize && uploadPath.equals("/ue/video/")){
                renderJson("state", "视频太大请控制在50MB以内");
                uploadFile.getFile().delete();
                return;
            }
            if(size > maxPostSize && uploadPath.equals("/ue/file/")){
                renderJson("state", "文件太大请控制在20MB以内");
                uploadFile.getFile().delete();
                return;
            }

			String fileName = uploadFile.getFileName( );
			String extName = ImageKit.getExtName(fileName);



			/**
			 * ueditor 要求的返回格式：
			 * {"state": "SUCCESS",
			 * "title": "1465008328293017063.png",
			 * "original": "2222.png",
			 * "type": ".png",
			 * "url": "/ueditor/jsp/upload/image/20160604/1465008328293017063.png",
			 * "size": "185984" }
			 */
			String ip = PropKit.get("host") + Cons.prj;
			// renderJson(ret);
			renderJson(

					Ret.of("state", "SUCCESS")
							.set("url", ip+"/upload" + uploadPath + uploadFile.getFileName( ))
							.set("title", uploadFile.getFileName( ))
							.set("original", uploadFile.getOriginalFileName( ))
							.set("type", extName)
							.set("size", size));
		} catch (com.jfinal.upload.ExceededSizeException ex) {
			renderJson("state", "上传图片只允许 200K 大小");
		} catch (Exception e) {
			if (uploadFile != null) {
				uploadFile.getFile( ).delete( );
			}

			renderJson("state", "上传图片出现未知异常，请告知管理员：" + e.getMessage( ));
			LogKit.error(e.getMessage( ), e);
		}
	}
}
